package com.dinghui.pay.controller;

import com.dinghui.pay.common.ResponseResult;
import com.dinghui.pay.pojo.TbOrder;
import com.dinghui.pay.pojo.param.UserOrderParam;
import com.dinghui.pay.service.OrderService;
import com.dinghui.pay.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 订单业务
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 查询用户订单
     * @param param
     * @return
     */
    @RequestMapping(value = "/getUserOrders", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getUserOrders(@RequestBody UserOrderParam param, @RequestHeader HashMap header) {
        String token = header.get("token").toString();
        int userId = Integer.parseInt(jwtUtils.getClaimByToken(token).getSubject());
        Integer page = param.getPage();
        Integer length = param.getLength();
        int start = (page-1)*length;
        HashMap map = new HashMap();
        map.put("userId", userId);
        map.put("start", start);
        map.put("length", length);
        ArrayList<TbOrder> list = orderService.getUserOrderList(map);
        return ResponseResult.success(list);
    }

}
